<template>
  <div class="btntop">
    <div class="warp topbar">
      <!-- 左侧 -->
      <span>欢迎来到叩丁狼积分商城</span>
      <ul>
        <li>
          <img :src="userInfo.headImg" alt="" />
          用户名： {{ userInfo.nickName }}
        </li>
        <li>游客:</li>
        <li>我的积分: {{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ carTotal }}</strong>
        </li>
        <li class="last" @click="changeisShowloginModal(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { winxinloginAPI } from "@/request/api";
export default {
  async created() {
    // console.log("输出", this.$route.query.code);
    let mycode = this.$route.query.code;
    if (mycode) {
      let res = await winxinloginAPI({
        code: mycode,
      });

      if (res.code == 0) {
        // 提示
        alert("扫码登录成功");
        // 2.存token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        //  3.清除token
        this.$router.push(this.$route.path);
        // 4. 更新登陆状态
        this.changeLsLogined(true);
        // 5.扫码自动更新用户信息
        this.asyncChangeuserInfo();
      } else if (res.code == 400) {
        // 二维码失效,需要重新扫码
        // 显示重新扫码的提示
        alert("扫码已过期,请重新扫码");
        //  帮用户重新打开扫码登陆窗口
        this.changeisShowloginModal(true);
      } else if (res.code == 407) {
        alert("麻烦你在用手机号登录一次");
        this.changeisShowloginModal(true);
        localStorage.setItem("uuid", res.uuid);
        // console.log("收回数据为", res);
      }
    } else {
      // 检查是否登陆
      // console.log("......没有通过扫码就进来了");
      // 如果是没有扫码就进来的话
      let mytoken = localStorage.getItem("x-auth-token");
      this.changeLsLogined(Boolean(mytoken));

      // 如果当前未登录,用户的信息应该要初始化
      if (mytoken) {
        // 解决了登录过后刷新页面用户登录数据消失的问题
        this.asyncChangeuserInfo();
      } else {
        //  初始化用户数据
        // console.log("初始化");
        this.inituserInfo();
      }
    }
  },
  methods: {
    ...mapMutations({
      changeisShowloginModal: "showModal/changeisShowloginModal",
      changeLsLogined: "loginStatus/changeLsLogined",
      inituserInfo: "userInfo/inituserInfo",
    }),
    ...mapActions({
      asyncChangeuserInfo: "userInfo/asyncChangeuserInfo",
    }),
  },
  watch: {
    // 监听地址
    "$route.path": {
      handler(newVal, oldVal) {
        // console.log("监听结果", newVal, oldVal);
        // 重新获取token值
        let mytoken = localStorage.getItem("x-auth-token");
        // 监听token
        this.changeLsLogined(Boolean(mytoken));
      },
    },
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      carTotal: (state) => state.userInfo.carTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
};
</script>

<style lang="less" scoped>
.btntop {
  background-color: #242b39;
  .topbar {
    span {
      color: aliceblue;
    }
    display: flex;
    justify-content: space-between;
    height: 40px;
    line-height: 40px;
    background-color: #242b39;
    color: aliceblue;
    ul {
      list-style: none;
      text-align: center;
      cursor: pointer;
      li {
        float: left;
        margin-left: 20px;
      }
      .last {
        width: 124px;
        height: 40px;
        background: #0a328e;
      }
      img {
        width: 23px;
        height: 22px;
        border-radius: 13px;
        vertical-align: middle;
      }
      .btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        img {
          // margin-right: 10px;
          border-radius: 0;
          line-height: 22px;
          display: inline-block;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          vertical-align: middle;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>